<%@page import="java.util.LinkedList"%>
<%@page import="BusinessLogic.UserContainer"%>
<%@page import="BusinessLogic.User"%>
<%@page import="BusinessLogic.Result.ResultType"%>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.io.*" %>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="java.util.*" %>
<%@ page import="BusinessLogic.Result" %>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="public/style.css" rel="stylesheet" type="text/css" media="screen"/>
<title>EasySchedule</title>
<script type="text/javascript" src="public/jquery.js"></script>
<script type="text/javascript" src="public/userValidate.js"></script>
<script type="text/javascript">
function goToAcnhor() { 
	window.location.hash="errorAnchor"; 
}
</script>
</head>
<jsp:useBean id="requestHandler" class="RequestHandlerPackage.RequestHandler" scope="application"/>
<%
Map<String,String[]> params = new HashMap<String,String[]>();
String uName = request.getUserPrincipal().getName();
params.put("action",new String[]{"getUser"});
params.put("USER_NAME",new String[]{uName});
params.put("me",new String[]{uName});
Result res = requestHandler.handle(params);
boolean admin = false;
User user=null;
if (res.getResultType().equals(ResultType.SUCCESS))
{
	if (((User)(res.getSuccessReturnValue())).getRole().equals("admin")){
		admin = true;
	}
	user = (User)res.getSuccessReturnValue();
}
String uname,pass,fname,lname,email,phone;
uname = pass = fname = lname = email = phone = "";
String error="";
if (request.getMethod().equals("POST"))
{
	MultipartRequest req = new MultipartRequest(request,getServletContext().getRealPath("/")+File.separator+"public"+File.separator+"images");
	params.clear();
	Enumeration<String> map = req.getParameterNames();
	params.put("action", new String[]{"updateUserDetails"});
	while(map.hasMoreElements())
	{
		String par = (String) map.nextElement();
		params.put(par, req.getParameterValues(par));
	}
	params.put("ROLE", new String[]{"student"});
	if (admin)
		params.put("ROLE", new String[]{"admin"});
	params.put("USER_NAME", new String[]{request.getUserPrincipal().getName()});
	res = requestHandler.handle(params);
	if (res.getResultType().equals(ResultType.SUCCESS))
			{
				String redirect = request.getContextPath() + "/";
				response.sendRedirect(redirect);
			}
	else{
		Map<String,String[]> m = (Map<String,String[]>)res.getFailureReturnValue();
		error = m.get("error")[0];
	}
}
else if (request.getMethod().equals("GET")){
	uname = user.getName();
	email = user.getEmail();
	fname = user.getFirst_name();
	lname = user.getLast_name();
	phone = user.getPhone();
}
String anchor = error.isEmpty()?"": "onLoad=\"goToAcnhor()\"";
%>
	<body <%=anchor %>>>
	   <div id="page">
	        <div id="header">
	            <h1><a href="./">Easy<span class="logo">Schedule</span></a></h1>
	            <p id="subtitle">Nice and easy registration system</p>
	        </div>
	        <div id="menu">
	            <ul>
	                <li class="left_menu"><a href="./">Home</a></li>
	                <li class="left_menu"><a href="./about.html">About</a></li>
	                <li class="left_menu"><a href="/tamirandevyatar/logout">Logout</a></li>
	            </ul>
	        </div>
	        <div id="main">	
	            <div id="sidebar">
	                <div class="box" id="sidebar_head">
	                <%if (admin){ %>
	                    <h2><a id="loginAnchor">Admin</a></h2>
	                     <%}else{ %>
	                      <h2><a id="loginAnchor">User</a></h2>
	                     <%} %>
	                    <div class="box2">
                        <ul>
                        	<%if (admin){ %>
                            <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/addCourse">Add course</a></li>
                            <li><a href="/tamirandevyatar/users">Manage users</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                            <%}else{ %>
                            <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/search">Search courses</a></li>
                            <li><a href="/tamirandevyatar/timetable">My timetable</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                            <li><a href="/tamirandevyatar/users">View users</a></li>
                            <%} %>
                        </ul>
                        </div>
	                </div>		
	            </div><!-- sidebar -->    	              
	            <div id="content">
	                <div id="contheader">
	                    <h2>Update your details</h2>
	                </div>		
	                <div id="content2" class="welcome">
	                	<div class="entry">
						<form id = "reg" action="" method="post" enctype="multipart/form-data" onsubmit="return validateForm('update')">
							<label id="generalError" class="error"><%=error%></label>
							<p><a id="errorAnchor"></a>Please fill out the information below to sign up: (* marks required fields):</p>
					    	<table border="0">
							<tr>
							<td>
					    	<label for="USER_NAME">* Username &nbsp;&nbsp;</label>
					    	</td>
					    	<td>
					    	<input type="text" disabled="disabled" name="USER_NAME" value="<%=uname%>" id="USER_NAME"/>
					    	<label id="USERERROR" class="error"></label>
					    	</td>
					    	</tr>
					    	<tr>
					    	<td>
					    	<label for="PASSWORD">* Password &nbsp;&nbsp;</label>
					    	</td>
					    	<td>
					    	<input type="password" name="PASSWORD" value="" id="PASSWORD"/>
	              	        <label id="PASSERROR" class="error"></label>
							</td>
							</tr>
							<tr>
							<td>
							<label for="passwordConfirmation">* Password Confirmation&nbsp;&nbsp;</label>
					    	</td>
					    	<td>
					    	<input type="password" name="passwordConfirmation" value="" id="passwordConfirmation"/>
							<label id="PASSCONFERROR" class="error"></label>
	                        </td>
	                        </tr>
							<tr>
							<td>
							<label for="FIRST_NAME">First Name&nbsp;&nbsp;</label>
					    	</td>
					    	<td>
					    	<input type="text" name="FIRST_NAME" value="<%=fname%>" id="FIRST_NAME"/>
					    	<label id="FIRSTERROR" class="error"></label>
							</td>
							</tr>
							<tr>
							<td>
							<label for="LAST_NAME">Last Name&nbsp;&nbsp;</label>
					    	</td>
					    	<td>
					    	<input type="text" name="LAST_NAME" value="<%=lname%>" id="LAST_NAME"/>
					    	<label id="LASTERROR" class="error"></label>
							</td>
							</tr>
							<tr>
							<td>
							<label for="EMAIL">Email&nbsp;&nbsp;</label>
					    	</td>
					    	<td>
					    	<input type="text" name="EMAIL" value="<%=email%>" id="EMAIL" class="right"/>
					    	<label id="EMAILERROR" class="error"></label>
							</td>
							</tr>
							<tr>
							<td>
							<label for="PHONE">Phone number&nbsp;&nbsp;</label>
					    	</td>
					    	<td>
					    	<input type="text" name="PHONE" value="<%=phone%>" id="PHONE"/>
					    	<label id="PHONEERROR" class="error"></label>
							</td>
							</tr>
							</table>
						    <h2>
						    	<input type="submit" value="Update" />
						    	<input type="reset" value="Reset" />
					    	</h2>
				  		</form>
					</div>
                </div><!-- content2 -->								              
            </div><!-- content -->                    
            <div class="clearing">&nbsp;</div> 
            <div id="footer">
                <p><a href="http://www.cs.technion.ac.il/~cs236369/">Managing Data on the World-Wide Web</a></p>
            </div>
        </div><!-- main -->
    </div><!-- page -->
	</body>
</html>	